
local fontDefault = "core/resources/fonts/1_3.ttf"
LabelSizeMode =
    Enum {
    "Custom",
    "Row"
}
LabelCacheMode = Enum{
    "Char",
    "Full"
}

Label = {
    props = {
        _fontSize = 20,
        _cacheMode = LabelCacheMode.Char,
        _labelSizeMode = LabelSizeMode.Row
    },
    dependencies = {
        Renderer
    },
    gets = {
        fontSize = function (self)
            return self._fontSize
        end,
        labelSizeMode = function(self)
            return self._labelSizeMode
        end,
        string = function(self)
            return self._string
        end,
    },
    sets = {
        fontSize = function (self, key, val)
            self._fontSize = val
            self.font = love.graphics.newFont(fontDefault, val)
            self._text = love.graphics.newText(self.font, self._string)
            if self.labelSizeMode == LabelSizeMode.Row then
                self:updateSizeByText()
            end
        end,
        labelSizeMode = function(self, key, val)
            self._labelSizeMode = val
            if val == LabelSizeMode.Row then
                self:updateSizeByText()
            end
        end,
        string = function(self, key, val)
            self._string = val
            self._text:set(val)
            if self._labelSizeMode == LabelSizeMode.Row then
                self:updateSizeByText()
            end
        end
    }
}

---@diagnostic disable-next-line: duplicate-set-field
function Label:ctor(str)
    self._string = str
    self.font = love.graphics.newFont(fontDefault, 20)
    self._text = love.graphics.newText(self.font, str)
    self.color = Color.new(0, 0, 0, 1)
    self:updateSizeByText()
    self.style = Styles.label
end

function Label:updateSizeByText()
    self:wh(self._text:getDimensions())
end

function Label:createDefaultMaterial()
    return Material.textDefault
end

function Label:getRenderInfo()
    if self.renderInfoDirty then
        local wx, wy, wr, wsx, wsy, kx, ky = self:getDrawInfo()
        self._renderInfo = {
            renderType = RenderType.Text,
            text = self._text,
            color = self.color,
            args = {wx, wy, wr, wsx, wsy, 0, 0,kx, ky}
        }
        self._renderInfo.cmp = self
        self.renderInfoDirty = false
    end
    return self._renderInfo
end

function Node.Label(str)
    local node = Node.new()
    node:addComponent(Label, str)
    return node
end
